package com.sanpu.kxe.business.trade.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.trade.bean.PayCollectionOutput;
import com.sanpu.kxe.common.utils.CommonUtils;
import com.sanpu.kxe.module.roundview.RoundTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：收款渠道
 * Created by 薛勇军 on 2019/3/1.
 */

public class ChoosePayAdapter extends BaseAdapter {

    private List<PayCollectionOutput> listData = new ArrayList<>();

    private Context context;

    public ChoosePayAdapter(List<PayCollectionOutput> listData, Context context) {
        this.listData = listData;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (listData == null)
            return 0;
        return listData.size();
    }

    @Override
    public PayCollectionOutput getItem(int position) {
        if (listData == null)
            return null;
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_pay_details, null);
        }
        TextView tvItemTitle = ButterKnife.findById(convertView, R.id.tvItemTitle);
        ImageView ivItemPay = ButterKnife.findById(convertView, R.id.ivItemPay);
        TextView tvItemRemark = ButterKnife.findById(convertView, R.id.tvItemRemark);
        TextView tvItemMyFee = ButterKnife.findById(convertView, R.id.tvItemMyFee);
        LinearLayout hintLayout = ButterKnife.findById(convertView, R.id.hintLayout);
        TextView hintTv = ButterKnife.findById(convertView, R.id.hintTv);
        ImageView stratumIv = ButterKnife.findById(convertView, R.id.stratumIv);
        RoundTextView hintRtv = ButterKnife.findById(convertView, R.id.hintRtv);
        ImageView chooseIv = ButterKnife.findById(convertView, R.id.chooseIv);

        PayCollectionOutput categorysBean = getItem(position);
        if (categorysBean != null) {
            if (!TextUtils.isEmpty(categorysBean.getTitle()))
                tvItemTitle.setText(categorysBean.getTitle());
            try {
                ivItemPay.setImageResource(CommonUtils.getMemberPayDrawable(categorysBean.getShowIcon()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            tvItemMyFee.setVisibility(View.GONE);
            if (categorysBean.getRateDetails() != null && !categorysBean.getRateDetails().isEmpty()) {
                PayCollectionOutput.RateDetail rateDetail = categorysBean.getRateDetails().get(0);
                if (rateDetail != null) {
                    tvItemMyFee.setText("我的手续费：" +
                            rateDetail.getTradeRate() + "%+" + rateDetail.getExtraCharge() + " 元/笔");
                    tvItemMyFee.setVisibility(View.VISIBLE);
                }
            }
            String itemRemark = "额度 " + categorysBean.getMinTradeAmount()
                    + "~" + categorysBean.getMaxTradeAmount() + "元，时间 " + categorysBean.getTradeStartTime()
                    + "~" + categorysBean.getTradeEndTime();
            tvItemRemark.setText(itemRemark);
            tvItemRemark.setVisibility(View.VISIBLE);

            if (categorysBean.isHot()) {
                tvItemTitle.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(context, R.drawable.ic_payment_hot), null);
            } else {
                tvItemTitle.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            }
            hintLayout.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(categorysBean.getPrompt())) {
                hintLayout.setVisibility(View.VISIBLE);
                hintTv.setText(categorysBean.getPrompt());
            }
            if (categorysBean.isChoose()) {
                chooseIv.setImageResource(R.drawable.ic_choose_checked);
            } else {
                chooseIv.setImageResource(R.drawable.ic_choose_noraml);
            }
            if (TextUtils.equals("DISABLE", categorysBean.getEnableStatus())) {
                stratumIv.setVisibility(View.VISIBLE);
                chooseIv.setVisibility(View.GONE);
                tvItemTitle.setTextColor(ContextCompat.getColor(context, R.color.c_cecece));
                tvItemRemark.setTextColor(ContextCompat.getColor(context, R.color.c_cecece));
                tvItemMyFee.setTextColor(ContextCompat.getColor(context, R.color.c_cecece));
                hintTv.setTextColor(ContextCompat.getColor(context, R.color.c_cecece));
                hintRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(context, R.color.c_cecece));
            } else {
                stratumIv.setVisibility(View.GONE);
                chooseIv.setVisibility(View.VISIBLE);
                tvItemTitle.setTextColor(ContextCompat.getColor(context, R.color.c_4a4a4a));
                tvItemRemark.setTextColor(ContextCompat.getColor(context, R.color.c_a2a2a2));
                hintTv.setTextColor(ContextCompat.getColor(context, R.color.c_ff3f40));
                hintRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(context, R.color.c_ff3f40));
            }
            convertView.setTag(categorysBean);
        }
        return convertView;
    }

    public void setNotifyData(List<PayCollectionOutput> data) {
        if (listData == null)
            listData = new ArrayList<>();
        listData.clear();
        if (data != null) {
            listData.addAll(data);
        }
        this.notifyDataSetChanged();
    }
}
